<?php
if (!(defined('IN_IA'))) 
{
	exit('Access Denied');
}
class Index_Page extends WebPage
{

	public function type() 
	{
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        $pindex = max(1, intval($_GPC['page']));
        $psize = 8;
        $list=pdo_fetchall("select * from ".tablename('bd_mis_product_type')." where `uniacid`='$uniacid' order by type_sort desc LIMIT " . ($pindex - 1) * $psize . ",{$psize}");
        
        $total = pdo_fetchcolumn("select count(*) from ".tablename('bd_mis_product_type')." where `uniacid`='$uniacid' order by type_sort desc");
        $pager = pagination($total, $pindex, $psize);
        include $this->template();
	}
    public function type_add() 
	{
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        load()->func('tpl');
        if($_W['ispost']){
            $data=array();
            $data['type_sort']=$_GPC['type_sort'];
            $data['type_logo']=$_GPC['type_logo'];
            $data['type_name']=$_GPC['type_name'];
            $data['uniacid']=$uniacid;
            $data['add_time']=date('Y-m-d H:i:s');                        $data['last_time']=date('Y-m-d H:i:s');
            $res=pdo_insert('bd_mis_product_type',$data);  
            if($_GPC['jixu'])
            {
                //继续添加
                $this->success('添加成功','',2);
            }
            else
            {
                $this->success('添加成功','product/index/type');
            }
        }
        include $this->template();
	}
    /*
    * 修改分类
     */
    public function type_edit()
    {
        global $_W,$_GPC;
        load()->func('tpl');
        $id=$_GPC['type_id'];
        $uniacid=$_W['uniacid'];
        $data=pdo_get('bd_mis_product_type',array('type_id'=>$id,'uniacid'=>$uniacid));
        
        if($_W['ispost']){
            $data=array();
            $data['type_sort']=$_GPC['type_sort'];
            $data['type_logo']=$_GPC['type_logo'];
            $data['type_name']=$_GPC['type_name'];                        $data['last_time']=date('Y-m-d H:i:s');
            $res=pdo_update('bd_mis_product_type',$data,array('type_id'=>$id));
            $this->success('修改成功','product/index/type');
        }
        include $this->template();
    }
    /*
    * 删除分类
     */
    public function type_del()
    {
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        if($_GPC['type_id']){
            $id=$_GPC['type_id'];
            $data=pdo_delete('bd_mis_product_type',array('type_id'=>$id,'uniacid'=>$uniacid));
            $this->success('删除成功','product/index/type');
        }
    }
    /*
    * 产品
     */
    public function product() 
	{
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        $name=$_GPC['product_name'];
        $where='';
        if($name!='')
        {
            $where = " and a.product_name like'%".$name."%'";
        }
        $pindex = max(1, intval($_GPC['page']));
        $psize = 8;
        $list=pdo_fetchall("select a.*,b.type_name from ".tablename('bd_mis_product')."  as a Left Join" .tablename('bd_mis_product_type')." as b On a.product_type=b.type_id where a.`uniacid`='$uniacid' $where order by a.product_id desc LIMIT " . ($pindex - 1) * $psize . ",{$psize}");
        
        $total = pdo_fetchcolumn("select count(*) from ".tablename('bd_mis_product')."  as a Left Join" .tablename('bd_mis_product_type')." as b On a.product_type=b.type_id where a.`uniacid`='$uniacid' $where order by a.product_id desc");
        $pager = pagination($total, $pindex, $psize);
        $i=($pindex - 1) * $psize+1;
        include $this->template();
	}
    /*
    * 添加产品
     */
    public function product_add() 
	{
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        load()->func('tpl');
        if($_W['ispost']){
            $data=array();
            $recom = $_GPC['is_recom']=='1'?'1':'0';
            $data['is_recom']=$recom;
            $data['product_thumb']=$_GPC['product_thumb'];
            $data['product_name']=$_GPC['product_name'];
            $data['product_current_price']=$_GPC['product_current_price'];
            $data['product_type']=$_GPC['product_type'];
            $data['product_content']=$_GPC['product_content'];
            $data['uniacid']=$uniacid;
            $data['last_time']=date('Y-m-d H:i:s');                        $data['add_time']=date('Y-m-d H:i:s');
//             if (is_array($_GPC['figure_img'])) 
//             {
//                 $thumbs = $_GPC['figure_img'];
//                 $thumb_url = array();
//                 foreach ($thumbs as $th ) 
//                 {
//                     $thumb_url[] = tomedia($th);
//                 }
//                 $data['figure_img'] = json_encode($thumb_url);
//             }
            $res=pdo_insert('bd_mis_product',$data);  
            if($_GPC['jixu'])
            {
                //继续添加
                $this->success('添加成功','',2);
            }
            else
            {
                $this->success('添加成功','product/index/product');
            }
        }
        $product_type=pdo_fetchall("select * from ".tablename('bd_mis_product_type')." where `uniacid`='$uniacid'");
        include $this->template();
	}
    /*
    * 修改产品信息
     */
    public function product_edit()
    {
        global $_W,$_GPC;
        load()->func('tpl');
        $id=$_GPC['product_id'];
        $uniacid=$_W['uniacid'];
        $product=pdo_get('bd_mis_product',array('product_id'=>$id,'uniacid'=>$uniacid));
        if($_W['ispost']){
            $data=array();
            $recom = $_GPC['is_recom']=='1'?'1':'0';            $data['is_recom']=$recom;            $data['product_thumb']=$_GPC['product_thumb'];            $data['product_name']=$_GPC['product_name'];            $data['product_current_price']=$_GPC['product_current_price'];            $data['product_type']=$_GPC['product_type'];            $data['product_content']=$_GPC['product_content'];            $data['last_time']=date('Y-m-d H:i:s');                        if (is_array($_GPC['figure_img'])) 
            {
                $thumbs = $_GPC['figure_img'];
                $thumb_url = array();
                foreach ($thumbs as $th ) 
                {
                    $thumb_url[] = tomedia($th);
                }
                $data['figure_img'] = json_encode($thumb_url);
            }
            $res=pdo_update('bd_mis_product',$data,array('product_id'=>$id));
            $this->success('修改成功','product/index/product');
        }
        $product_type=pdo_fetchall("select * from ".tablename('bd_mis_product_type')." where `uniacid`='$uniacid'");
        include $this->template();
    }
    /*
    * 删除产品
     */
    public function product_del()
    {
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        if($_GPC['id']){
            $id=$_GPC['id'];
            $data=pdo_delete('bd_mis_product',array('id'=>$id,'uniacid'=>$uniacid));
            $this->success('删除成功','product/index/product');
        }
    }
    /*
     * 批量处理产品
     */
    public function product_editall()
    {
        global $_W,$_GPC;
        $uniacid=$_W['uniacid'];
        if(empty($_GPC['xuanze']))
        {
            $this->error('未选择数据','',0);
        }
        if($_GPC['piliang'] == 1)
        {
            //批量删除
            foreach ($_GPC['xuanze'] as $key => $value) {
                $data['id']=$value;
                $data['uniacid'] = $uniacid;
                pdo_delete('bd_mis_product',$data);
            }
            $this->success('删除成功','',2);
        }
        else if($_GPC['piliang'] == 2)
        {
            //批量显示
            foreach ($_GPC['xuanze'] as $key => $value) {
                $data['recom'] = 1;
                pdo_update('bd_mis_product',$data,array('id'=>$value,'uniacid'=>$uniacid));
            }
            $this->success('处理成功','',2);
        }
        else
        {
            //批量不显示  
            foreach ($_GPC['xuanze'] as $key => $value) {
                $data['recom'] = 2;
                pdo_update('bd_mis_product',$data,array('id'=>$value,'uniacid'=>$uniacid));
            }
            $this->success('处理成功','',2);
        }
    }
     //修改推荐状态
    public function appoint_ajax_recom()
    {
        global $_W,$_GPC;
        load()->func('tpl');
        $id=$_GPC['id'];
        $uniacid=$_W['uniacid'];
        $data=pdo_get('bd_mis_product',array('id'=>$id,'uniacid'=>$uniacid));
        $sale = $data['recom'] == '1'? '2' : '1';
        $array = array('recom'=>$sale);
        $res=pdo_update('bd_mis_product',$array,array('id'=>$id,'uniacid'=>$uniacid));
        echo $sale; 
        exit;   
    }
    /*
     * 去除富文本内的字体
     * 2018年1月19日13:22:00
     */
    public function fwb_del_font($str)
    {
        $str = htmlspecialchars_decode($str);
        $re = preg_replace("/font-family:.*;/i",'',$str);
        $re = preg_replace("/font-family:.*\"/i",'"',$re);
        return $re;
    }
}
?>